<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import selectLevel from "../selectLevel/index.js";

export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base],
  components: {
    SelectDataGoods: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoods()),
  },
  data() {
    return {
      appWindow: window,
      form: {
        isOpen: 0, //是否开启关系链
        conditionType: 1, // 获取推广资格条件
        qualified: 1, //是否同时满足
        conditionNode: 1, //消费条件统计方式
        relationNode: 1, //锁定客户的标准

        // 关注公众号
        subscribeWeChatMp: {
          isSelect: 0,
          conditionValue: "",
        },

        // 会员等级
        memberLevel: {
          isSelect: 0,
          conditionValue: [],
        },

        // 购买指定商品
        buyProduct: {
          isSelect: 0,
          conditionValue: [],
        },

        // 消费次数
        consumeCount: {
          isSelect: 0,
          conditionValue: "",
        },

        // 消费金额
        consumeAmount: {
          isSelect: 0,
          conditionValue: "",
        },

        // 充值次数
        rechargeCount: {
          isSelect: 0,
          conditionValue: "",
        },

        // 充值金额
        rechargeAmount: {
          isSelect: 0,
          conditionValue: "",
        },
      },

      apiUrl: {
        detail: "mall-site-portal/member-relation-config/getItem",
        update: "mall-site-portal/member-relation-config/saveOrUpdate",
        search: "mall-site-portal/member-relation-config/getMemberLevelByIds",
      },

      goodsIds: [], //选择了的商品id数组

      checkedLevelList: [], //选择了用户等级的id数组

      load: true,

      // 用于显示的用户等级名称数组
      showLevelNameList: [],
    };
  },
  watch: {
    goodsIds: {
      handler(newVal, oldVal) {
        this.form.buyProduct.conditionValue = newVal;
      },
    },
  },
  methods: {
    init() {
      this.getDetail();
    },
    //   保存设置
    async submit() {
      if (!(await this.validate())) {
        return;
      }

      const {
        isOpen,
        conditionType,
        qualified,
        conditionNode,
        relationNode,
        subscribeWeChatMp,
        memberLevel,
        buyProduct,
        consumeCount,
        consumeAmount,
        rechargeCount,
        rechargeAmount,
      } = this.form;

      // 如果选了购买指定商品  但是没有选择商品
      if (buyProduct.isSelect === 1 && buyProduct.conditionValue.length === 0) {
        this.$vFN.Tools.messageError("至少选择一项商品");
        return;
      }

      // 如果选择了满足任一条件或者全部满足
      if (qualified != 1) {
        if (
          !subscribeWeChatMp.isSelect &&
          !memberLevel.isSelect &&
          !buyProduct.isSelect &&
          !consumeCount.isSelect &&
          !consumeAmount.isSelect &&
          !rechargeCount.isSelect &&
          !rechargeAmount.isSelect
        ) {
          this.$vFN.Tools.messageError("至少选择一个条件");
          return;
        }
      }

      console.log(memberLevel.conditionValue.length);
      if (memberLevel.isSelect && memberLevel.conditionValue.length === 0) {
        this.$vFN.Tools.messageError("至少选择一个等级");
        return;
      }

      this.load = true;

      let dataObj = {
        isOpen,
        conditionType,
        qualified,
        conditionNode,
        relationNode,
        relationCondition: {
          subscribeWeChatMp,
          memberLevel,
          buyProduct,
          consumeCount,
          consumeAmount,
          rechargeCount,
          rechargeAmount,
        },
      };

      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.update,
          data: dataObj,
        },
        window
      );

      if (res._ok) {
        this.$vFN.Tools.messageSuccess(res.message);
        this.load = false;
        this.getDetail();
      }
    },

    // 获取设置
    async getDetail() {
      this.load = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.detail,
        },
        window
      );

      if (res._ok) {
        let data_ = res.data;

        for (const key in this.form) {
          if (!this.$vFN.Utils.isEmpty(data_[key])) {
            this.form[key] = data_[key];
          }
        }

        const { relationCondition } = data_;

        for (const key in relationCondition) {
          if (!this.$vFN.Utils.isEmpty(relationCondition[key])) {
            this.form[key] = relationCondition[key];
          }
        }

        console.log(this.form);

        this.goodsIds = this.form.buyProduct.conditionValue;

        if (this.form.memberLevel.conditionValue.length > 0) {
          this.getLevelName(this.form.memberLevel.conditionValue);
        }

        this.load = false;
      }
    },

    // 选择会员等级
    selectLevel() {
      selectLevel.init({
        checkedLevelList: this.form.memberLevel.conditionValue,
        onSuccess: (res) => {
          console.log("res", res); //返回一个数组
          this.form.memberLevel.conditionValue = res;

          if (res.length > 0) {
            // 根据id获取会员等级名称
            this.getLevelName(this.form.memberLevel.conditionValue);
          } else {
            this.showLevelNameList = [];
          }
        },
      });
    },

    // 删除等级
    deleteTag(index) {
      this.form.memberLevel.conditionValue.splice(index, 1);
      this.showLevelNameList.splice(index, 1);
    },

    // 获取等级名称
    async getLevelName(idArr) {
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.search,
          data: {
            memberLevelIds: idArr,
          },
        },
        window
      );
      if (res._ok) {
        this.showLevelNameList = res.data;
      }
    },

    // 验证方法开始 =======
    //消费次数
    validatorConsumeCount(rule, value, callback) {
      const { consumeCount } = this.form;
      if (value === "" && consumeCount.isSelect === 1) {
        callback(new Error("不能为空"));
      } else if (consumeCount.isSelect === 0) {
        consumeCount.conditionValue = "";
        callback();
      } else {
        this.$vFN.Tools.validateStock(rule, value, callback);
      }
    },

    // 消费金额
    validatorConsumeAmount(rule, value, callback) {
      const { consumeAmount } = this.form;
      if (value === "" && consumeAmount.isSelect === 1) {
        callback(new Error("不能为空"));
      } else if (consumeAmount.isSelect === 0) {
        consumeAmount.conditionValue = "";
        callback();
      } else {
        this.$vFN.Tools.validateStock(rule, value, callback);
      }
    },

    // 充值次数
    validatorRechargeCount(rule, value, callback) {
      const { rechargeCount } = this.form;
      if (value === "" && rechargeCount.isSelect === 1) {
        callback(new Error("不能为空"));
      } else if (rechargeCount.isSelect === 0) {
        rechargeCount.conditionValue = "";
        callback();
      } else {
        this.$vFN.Tools.validateStock(rule, value, callback);
      }
    },

    // 充值金额
    validatorRechargeAmount(rule, value, callback) {
      const { rechargeAmount } = this.form;
      if (value === "" && rechargeAmount.isSelect === 1) {
        callback(new Error("不能为空"));
      } else if (rechargeAmount.isSelect === 0) {
        rechargeAmount.conditionValue = "";
        callback();
      } else {
        this.$vFN.Tools.validateStock(rule, value, callback);
      }
    },

    validate() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$refs["editForm"].validate((valid) => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },
  },
  mounted() {
    this.init();
  },
};
</script>
